#!/bin/bash

function print_prompt
{
  cat << CONFEOF
Usage: $0 [OPTION]... [VAR=VALUE]...

Configuration:
    --help                      display this help and exit
    --debug                     build for debug
    --test                      build unit-tests
    --build-dir=DIR             build directory
    --prefix=PREFIX             install prefix
    --cmake-modules=DIR         directory of cmake modules file exist
    --find-root-path=DIR        root dir for search dependencies libs
    --svc-log-level=*           log level of flora service(verbose|debug|info|warning|error|none)
    --cli-log-level=*           log level of flora client(verbose|debug|info|warning|error|none)
    --use-gcc                   force use gcc compiler (in macosx)

Dependencies:
    --mutils=DIR                specify mutils install dir
    --ncurses=DIR               specify ncurses install dir

Cross Compile:
    --toolchain=DIR             toolchain install dir
    --cross-prefix=PREFIX       compiler name prefix
CONFEOF
}

builddir="build"
cmake_modules_dir="cmake"

CMAKE_ARGS=
for conf_opt
do
  case $conf_opt in
    *=?*) conf_optarg=`expr "X$conf_opt" : '[^=]*=\(.*\)'` ;;
    *) conf_optarg= ;;
  esac

  case $conf_opt in
    --help)
      print_prompt
      exit 0
      ;;
    --debug)
      CMAKE_ARGS=(${CMAKE_ARGS[@]} -DBUILD_DEBUG=ON)
      ;;
    --test)
      CMAKE_ARGS=(${CMAKE_ARGS[@]} -DBUILD_TEST=ON)
      ;;
    --build-dir=*)
      builddir=$conf_optarg
      ;;
    --prefix=*)
      CMAKE_ARGS=(${CMAKE_ARGS[@]} -DCMAKE_INSTALL_PREFIX=$conf_optarg)
      ;;
    --cmake-modules=*)
      cmake_modules_dir=$conf_optarg
      ;;
    --svc-log-level=*)
      CMAKE_ARGS=(${CMAKE_ARGS[@]} -DSVC_LOGLEVEL=$conf_optarg)
      ;;
    --cli-log-level=*)
      CMAKE_ARGS=(${CMAKE_ARGS[@]} -DCLI_LOGLEVEL=$conf_optarg)
      ;;
    --use-gcc)
      CMAKE_ARGS=(${CMAKE_ARGS[@]} -DUSE_GCC=ON)
      ;;
    --mutils=*)
      CMAKE_ARGS=(${CMAKE_ARGS[@]} -DmutilsPrefix=$conf_optarg)
      ;;
    --ncurses=*)
      CMAKE_ARGS=(${CMAKE_ARGS[@]} -DncursesPrefix=$conf_optarg)
      ;;
    --toolchain=*)
      CMAKE_ARGS=(${CMAKE_ARGS[@]} -DTOOLCHAIN_HOME=$conf_optarg)
      CROSS_COMPILE=yes
      ;;
    --cross-prefix=*)
      CMAKE_ARGS=(${CMAKE_ARGS[@]} -DCROSS_PREFIX=$conf_optarg)
      ;;
    --find-root-path=*)
      CMAKE_ARGS=(${CMAKE_ARGS[@]} -DCMAKE_FIND_ROOT_PATH=$conf_optarg)
      ;;
  esac
done

CMAKE_ARGS=(${CMAKE_ARGS[@]} -DCUSTOM_CMAKE_MODULES=$cmake_modules_dir)
CUR_DIR=`pwd`
if [ x$CROSS_COMPILE = x"yes" ]; then
CMAKE_ARGS=(${CMAKE_ARGS[@]} -DCMAKE_TOOLCHAIN_FILE=$cmake_modules_dir/toolchain.cmake)
fi

mkdir -p $builddir
cd $builddir
cmake $CUR_DIR \
  ${CMAKE_ARGS[@]}
